/**
 * 
 */
package org.yelongframework.servlet.mvc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.yelongframework.lang.Nullable;
import org.yelongframework.servlet.http.CookieSupport;
import org.yelongframework.servlet.http.HttpServletRequestIPResolver;
import org.yelongframework.servlet.http.HttpServletResponseSupport;

/**
 * 抽象的控制器
 * 
 * @since 1.0.0
 */
public abstract class AbstractController implements Controller {

	/** 默认响应内容类型 */
	public static final String DEFAULT_RESPONSE_CONTENT_TYPE = "text/html;charset=UTF-8";

	// ==================================================constructor==================================================

	public AbstractController() {
	}

	// ==================================================响应文本==================================================

	/**
	 * 响应文本内容
	 * 
	 * @param text 内容
	 * @throws IOException 响应异常
	 */
	protected void responseText(String text) throws IOException {
		getResponseSupport().responseText(getResponse(), text, DEFAULT_RESPONSE_CONTENT_TYPE);
	}

	/**
	 * 响应文本内容
	 * 
	 * @param text        内容
	 * @param contentType 内容类型
	 * @throws IOException 响应异常
	 */
	protected void responseText(String text, String contentType) throws IOException {
		getResponseSupport().responseText(getResponse(), text, contentType);
	}

	/**
	 * 响应字节内容
	 * 
	 * @param content 内容字节流
	 * @throws IOException 响应异常
	 */
	protected void responseContent(byte[] content) throws IOException {
		getResponseSupport().responseByteArray(getResponse(), content);
	}

	// ==================================================响应文件==================================================

	/**
	 * 响应文件
	 * 
	 * @param response 响应
	 * @param filePath 文件路径
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(String filePath) throws IOException {
		getResponseSupport().responseFile(getResponse(), new File(filePath));
	}

	/**
	 * 响应文件
	 * 
	 * @param file 文件
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(File file) throws IOException {
		getResponseSupport().responseFile(getResponse(), file, file.getName());
	}

	/**
	 * 响应文件
	 * 
	 * @param filePath 文件路径
	 * @param fileName 文件名称
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(String filePath, @Nullable String fileName) throws IOException {
		getResponseSupport().responseFile(getResponse(), new File(filePath), fileName);
	}

	/**
	 * 响应文件
	 * 
	 * @param file     文件
	 * @param fileName 文件名称
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(File file, @Nullable String fileName) throws IOException {
		getResponseSupport().responseFile(getResponse(), new FileInputStream(file), fileName);
	}

	/**
	 * 响应文件
	 * 
	 * @param data     文件流
	 * @param fileName 文件名称
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(byte[] data, String fileName) throws IOException {
		getResponseSupport().responseFile(getResponse(), data, fileName);
	}

	/**
	 * 响应文件
	 * 
	 * @param inputStream 文件流
	 * @param fileName    文件名称
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(InputStream inputStream, String fileName) throws IOException {
		getResponseSupport().responseFile(getResponse(), inputStream, fileName);
	}

	/**
	 * 响应文件
	 * 
	 * @param inputStream 文件流
	 * @param bufferSize  缓冲区大小
	 * @param fileName    文件名称
	 * @throws IOException 响应文件异常
	 */
	protected void responseFile(InputStream inputStream, int bufferSize, String fileName) throws IOException {
		getResponseSupport().responseFile(getResponse(), inputStream, bufferSize, fileName);
	}

	// ==================================================支持==================================================

	/**
	 * @return Cookie支持
	 */
	public abstract CookieSupport getCookieSupport();

	/**
	 * @return 响应支持
	 */
	public abstract HttpServletResponseSupport getResponseSupport();

	/**
	 * @return 请求IP支持
	 */
	public abstract HttpServletRequestIPResolver getRequestIPResolver();

}
